package com.kongwg.p2.service;

import com.kongwg.p2.bean.Customer;

public class CustomerList {
	private Customer[] customer; //用来保存客户对象的数组

	private int total = 0;//记录已经保存客户对象的数量
	
	/**
	 * 用来初始化customers数组的构造器
	 * @param totalCustomer
	 */
	public CustomerList(int totalCustomer) {
		customer = new Customer[totalCustomer];
		
	}
	
	
	/**
	 * 将指定的客户添加到数组中
	 * @param customer
	 * @return true ：添加成功，false： 添加失败
	 */
	public boolean addCustomer(Customer customer){
		if(total>=this.customer.length){
			return false;
		}
		this.customer[total++] = customer;
		return true;
	}
	/**
	 * 修改指定索引位置客户信息
	 * @param index
	 * @param customer
	 * @return true：修改成功，false：修改失败
	 */
	public boolean replaceCustomer(int index,Customer customer){
		if(index<0 || index>=total){
			return false;
		}
		this.customer[index]= customer;
		return true;
	}
	/**
	 * 删除指定位置客户信息
	 * @param index
	 * @return true：删除成功，false： 删除失败
	 */
	public boolean deleteCustomer(int index){
		
		if(index<0 || index>total){
			return false;
		}
		for(int i = index;i<total-1;i++){
			customer[i] = customer[i+1];
		}
		customer[total-1] = null;
		total--;
		return true;
	}
	/**
	 * 获取全部客户的信息
	 * @return
	 */
	public Customer[] getAllCustomers(){
		Customer[] custs = new Customer[total];
		for(int i = 0;i<custs.length;i++){
			custs[i] = customer[i];
		}
		return custs;
	}
	/**
	 * 获取固定位置客户信息
	 * @param index
	 * @return 找到元素返回，未找到放回null；
	 */
	public Customer getCustomer(int index){
		if(index <0 || index>total){
			return null;
		}
		
		
		return customer[index];
	}
	
	
	public int getTotal(){
		return total;
	}
	
	
	
		
	
}
